﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LoghogInterface.Commands;
using System.Windows.Input;
using System.Collections.ObjectModel;
using LoghogInterface.Models;
using LoghogInterface.Modules;
using LoghogInterface.Views;
using System.Windows;

namespace LoghogInterface.ViewModels
{
    /// <summary>
    /// Модель представления настроек
    /// </summary>
    class ViewModelSettings : ViewModelBase
    {
        private ParserConfig _parserConfig = new ParserConfig();
        private ModelConfigMailing _modelConfigMailing;
        private ModelConfigDatabase _modelConfigDatabase;
        private SettingsUserRightsSQLHelper _userRightsSqlHelper;
        private SettingsUserGroupsSQLHelper _userGroupsSqlHelper;
        private TagsSQLHelper _tagsSqlHelper;

        private int _idJournal;

        //своства для хранения данных предсталения
        #region User rights
        public ObservableCollection<ViewModelUpdateDataUsersRights> UserRights { get; set; }
        #endregion

        #region User groups
        public List<string> UserGroups { get; set; }
        #endregion

        #region Tags
        public ObservableCollection<string> Tags { get; set; }
        #endregion

        #region Fields Email
        public string SMTPServer 
        {
            get 
            {
                return _modelConfigMailing.Server;
            }
            set
            {
                _modelConfigMailing.Server = value;
            }
        }

        public string SMTPPort 
        {
            get 
            {
                return _modelConfigMailing.Port;
            }
            set
            {
                _modelConfigMailing.Port = value;
            }
        }

        public string Email
        {
            get
            {
                return _modelConfigMailing.Email;
            }
            set
            {
                _modelConfigMailing.Email = value;
            }
        }

        public List<string> MailingFilters { get; set;}
        #endregion

        #region Fields Database
        public string ServerDatabase 
        { 
            get
            {
                return _modelConfigDatabase.Server;
            }
            set
            {
                _modelConfigDatabase.Server = value;
            }
        }

        public string NameDatabase
        {
            get
            {
                return _modelConfigDatabase.NameDatabase;
            }
            set
            {
                _modelConfigDatabase.NameDatabase = value;
            }
        }

        public string UserDatabase 
        {
            get
            {
                return _modelConfigDatabase.User;
            }
            set
            {
                _modelConfigDatabase.User = value;
            }
        }

        public string PasswordDatabase 
        {
            get
            {
                return _modelConfigDatabase.Password;
            }
            set
            {
                _modelConfigDatabase.Password = value;
            }
        }
        #endregion

        public ViewModelSettings(int idJournal)
        {
            _idJournal = idJournal;
            //настройки email рассылки
            _modelConfigMailing = _parserConfig.GetMailingSettings();
            MailingFilters = _modelConfigMailing.Filters;

            //настройки базы данных
            _modelConfigDatabase = _parserConfig.GetDatabaseSettings();

            //таблица прав групп
            _userRightsSqlHelper = new SettingsUserRightsSQLHelper();
            UserRights = _userRightsSqlHelper.GetUserRights();

            //отображение списка групп
            _userGroupsSqlHelper = new SettingsUserGroupsSQLHelper();
            UserGroups = _userGroupsSqlHelper.GetUserGroups();

            //теги
            _tagsSqlHelper = new TagsSQLHelper();
            Tags = _tagsSqlHelper.GetTags(_idJournal);
        }

        #region Commands

        #region AddFilter
        private DelegateCommand _windowAddFilter;
        public ICommand WindowAddFilter
        {
            get
            {
                if (_windowAddFilter == null)
                {
                    _windowAddFilter = new DelegateCommand(this.ShowWindowAddFilter);
                }
                return _windowAddFilter;
            }
        }

        private void ShowWindowAddFilter()
        {
            AddFiltr AddFiltrWin = new AddFiltr();
            AddFiltrWin.Show();
        }
        #endregion

        #region Add tag
        private DelegateCommand _showAddTags;
        public ICommand ShowAddTag
        {
            get
            {
                if (_showAddTags == null)
                {
                    _showAddTags = new DelegateCommand(this.OpenAddTegs);
                }
                return _showAddTags;
            }
        }
        private void OpenAddTegs()
        {
            WindowAddTag windowAddTag = new WindowAddTag(_idJournal);
            windowAddTag.Show();
        }
        #endregion

        #region Apply Settings
        DelegateCommand _applySettings;
        public ICommand ApplySettings
        {
            get
            {
                if (_applySettings == null)
                {
                    _applySettings = new DelegateCommand(this.ApplyAllSettings);
                }
                return _applySettings;
            }
        }

        private void ApplyAllSettings()
        {
            this.ApplyDatabase();
            this.ApplyMailing();
            this.ApplyRights();
        }

        private void ApplyDatabase()
        {
            _parserConfig.SetDatabaseSettings(_modelConfigDatabase);
        }

        private void ApplyMailing()
        {
            _parserConfig.SetMailingSettings(_modelConfigMailing);
        }

        private void ApplyRights()
        {
            _userRightsSqlHelper.UpdateUserRights(UserRights);
        }
        #endregion

        #endregion
    }
}
